<?php

require_once '../common/table.php';


function cmp($a, $b)
{
    return ($a["fights"] < $b["fights"]);
}


function hero()
{	
	global $db_old, $db_new;
	
	// fetch the new rank value and calc the dph 
	
	$sql = "SELECT name,strength,rank FROM users WHERE citizen=14";
	$result = $db_new->query($sql);
	
	while($res = $result->fetchArray(SQLITE3_NUM)) {
		$name = $res[0];
		$strength = $res[1];
		$points = $res[2];
		
		$detail = getRank($points);
		$inf = (($detail[0]-1)/20 + 0.3) * (($strength / 10) + 40);
		$user[] = array('name' => $name, 'rank_new' => $points, 'rank_diff' => 0, 'inf' => $inf, 'fights' => 0);
	}

	// fetch old rank
	
	foreach ($user as $key => $row) {
		$name = $row["name"];
		$query = "SELECT rank FROM users WHERE name='" . $db_old->escapeString($name) . "'";
		$result = $db_old->querySingle($query);
		if ($result != NULL) {
			$user[$key]["rank_diff"] = ($user[$key]["rank_new"] - $result) * 10;
			$user[$key]["fights"] = $user[$key]["rank_diff"] / ($user[$key]["inf"]*2.2);
		}
	}

	usort($user, "cmp");
	
	foreach($user as $key => $row) {
		printf("<%d> [b]%s[/b], %d, %d\r\n", $key+1,
						$row["name"],
						$row["rank_diff"], 
						$row["fights"]
					);
	}
	
}



// main

$db_old = new SQLite3('../old_citizens.db');
$db_new = new SQLite3('../new_citizens.db');

hero();

$db_old->close();
$db_new->close();

?>
